/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.ilikes.tools.nosql.jpa.clients.indexs.index.hadoops;

import java.io.IOException;

import java.util.Collection;
import java.util.HashMap;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.IOContext;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.store.Lock;
import org.apache.lucene.store.LockFactory;
import org.apache.lucene.store.NativeFSLockFactory;
import org.apache.lucene.util.IOUtils;
import org.slf4j.Logger;

import cn.ilikes.tools.logs.Loggerfactory;
import cn.ilikes.tools.nosql.jpa.SysConfigs;
import cn.ilikes.tools.nosql.jpa.clients.indexs.index.LuceneHadoopIndexSource;
import cn.ilikes.tools.nosql.jpa.configs.indexs.IndexConfigs;

/**
 * This class implements a Lucene Directory on top of a general FileSystem.
 * Currently it does not support locking.
 */
public class HadoopFileSystemDirectory extends Directory {

	private Logger logger = Loggerfactory.instance(HadoopFileSystemDirectory.class);

	private FileSystem fileSystem;
	
	private Configuration conf;

	private Path directory;

	private int ioFileBufferSize = 2048;

	//	private Map<String,FileSystemIndexOutput>  fileSystemIndexOutputs=new HashMap<K, V>();

	private String getProperties(IndexConfigs indexConfigs, String key) {
		return indexConfigs.getProperties().getProperty(key);
	}

	public HadoopFileSystemDirectory(IndexConfigs indexConfigs) throws IOException {

		initFileSystem(indexConfigs);
		initlockFactory(indexConfigs);
		create();

	}

	private void initlockFactory(IndexConfigs indexConfigs) throws IOException {
		HadoopZooKeepLockFactory lockFactory = new HadoopZooKeepLockFactory(indexConfigs);
		setLockFactory(lockFactory);
	}

	private void initFileSystem(IndexConfigs indexConfigs) throws IOException {
		String luceneDirPath = getProperties(indexConfigs, SysConfigs.index_save_path);
		String fs_default_name = getProperties(indexConfigs, SysConfigs.index_hadoop_fs_default_name);
		String fs_default_replication = getProperties(indexConfigs, SysConfigs.index_hadoop_fs_default_replication);
		Loggerfactory.info(logger, "init Lucene hadoop  Index Source  fs_default_name " + fs_default_name + "  lucene Dir Path  " + luceneDirPath);
		Configuration conf = new Configuration();
		conf.set("fs.default.name", fs_default_name);
		conf.set("dfs.replication", fs_default_replication);
		conf.set("fs.hdfs.impl.disable.cache", "true");
		
		this.conf = conf;
		Path path_ = new Path(luceneDirPath);
		setPath(path_);
		FileSystem fileSystem = FileSystem.get(conf);
		this.fileSystem =fileSystem;
		
	}
	
	

	private void create() throws IOException {
		FileSystem  fileSystem =initFileSystem(conf);
		if (!fileSystem.exists(directory)) {
			//fileSystem.delete(directory);
			fileSystem.mkdirs(directory);
		}

		boolean isDir = false;
		try {
			FileStatus status = fileSystem.getFileStatus(directory);
			if (status != null) {
				isDir = status.isDir();
			}
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		if (!isDir) {
			throw new RuntimeException(directory + " is not a directory");
		}
	}

	private void setPath(Path path) {
		this.directory = path;
	}

	private FileSystem  initFileSystem(Configuration conf) throws IOException {
		
		FileSystem fileSystem = FileSystem.get(conf);
//		this.fileSystem =fileSystem;
		return fileSystem;
	}

	@Override
	public void close() throws IOException {
		System.out.println("===============close    systrm ==============================================");
//		fileSystem.close();
	}

	@Override
	public IndexOutput createOutput(String name, IOContext context) throws IOException {
		FileSystem  fileSystem =initFileSystem(conf);
		Path file = new Path(directory, name);
		if (fileSystem.exists(file) && !fileSystem.delete(file, true)) {
			// delete the existing one if applicable
			throw new IOException("Cannot overwrite index file " + file);
		} else {
			//			fileSystem.createNewFile(new Path(directory, "121212"+System.currentTimeMillis()));
			//			System.out.println("============================  createOutput  " + file.toString());
			if (!fileSystem.createNewFile(file))
				throw new IOException("Cannot create index file " + file);

		}

		return new FileSystemIndexOutput(fileSystem, file, ioFileBufferSize);
	}

	@Override
	public void deleteFile(String name) throws IOException {
		FileSystem  fileSystem =initFileSystem(conf);
		if (!fileSystem.delete(new Path(directory, name), false)) {
			throw new IOException("Cannot delete index file " + name);
		}
	}

	@Override
	public boolean fileExists(String name) throws IOException {
		FileSystem  fileSystem =initFileSystem(conf);
		return fileSystem.exists(new Path(directory, name));
	}

	@Override
	public long fileLength(String name) throws IOException {
		FileSystem  fileSystem =initFileSystem(conf);
		long length = fileSystem.getFileStatus(new Path(directory, name)).getLen();
		return length;
	}

	@Override
	public String[] listAll() throws IOException {
		FileSystem  fileSystem =initFileSystem(conf);
	
		FileStatus[] fileStatus = fileSystem.listStatus(directory);
		String[] result = new String[fileStatus.length];
		for (int i = 0; i < fileStatus.length; i++) {
			result[i] = fileStatus[i].getPath().getName();
		}
		return result;
	}

	@Override
	public IndexInput openInput(String name, IOContext context) throws IOException {
		FileSystem  fileSystem =initFileSystem(conf);
		
		return new FileSystemIndexInput(fileSystem, new Path(directory, name), name, ioFileBufferSize);

	}

	@Override
	public void sync(Collection<String> names) throws IOException {
	              
	}

	@Override
	public IndexInputSlicer createSlicer(String name, IOContext context) throws IOException {

		return super.createSlicer(name, context);
	}

}
